package com.ccl.iot;import java.util.ArrayList;

public class IOTMonitor<TYPE> implements IOTInterface.IOTMonitorInterface {
	private int iListenerCount = 0;
	private ArrayList<TYPE> iListeners = null;
	
	@Override
	public boolean isMonitoring(IOTMonitorInterface aMonitor) {
		return iListenerCount > 0;
	}

	@Override
	public int getMonitorListenerCount(IOTMonitorInterface aMonitor) {
		return iListenerCount;
	}
	
	protected boolean addMonitorListener(TYPE aListener) {
		if (iListeners == null){
			iListeners = new ArrayList<TYPE>();
		}else if (iListeners.contains(aListener)){
			return false;
		}
		
		if (iListeners.add(aListener)){
			iListenerCount = iListeners.size();
			
			return true;
		}else{
			return false;
		}
	}
	
	protected boolean removeMonitorListener(TYPE aListener) {
		if (iListeners != null){
			if (iListeners.remove(aListener)){
				iListenerCount = iListeners.size();
				
				return true;
			}
		}
		
		return false;
	}
	
	protected TYPE getMonitorListener(int aIndex){
		if (iListeners != null && aIndex < iListenerCount){
			return iListeners.get(aIndex);
		}else{
			return null;
		}
	}
}
